﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace FileCopy
{
    public class Config
    {
        public static List<f_Item> Items;
        private static string configPath = Application.StartupPath + "\\Config.xml";
        public static void Init()
        {
            Items = new List<f_Item>();
            if (File.Exists(configPath))
            {
                string info = FileHelper.TxtReader(configPath);
                Items = SerialiazeHelper.XmlDeserialize<List<f_Item>>(info);
            }
        }

        public static void Update(f_Item item)
        {
            f_Item _item = Items.Find(dat => { return dat.Name == item.Name; });
            if (_item == null)
                Items.Add(item);
            else
                _item.Update(item);
            Save();
        }

        public static bool Save()
        {
            try
            {
                string info = SerialiazeHelper.XmlSerialiaze(Items, Encoding.UTF8);
                FileHelper.WriteToTxt(configPath, info, true);
                return true;
            }
            catch (Exception ex)
            {
                ExceptionManager.TraceWrite(ex.Message, "保存配置");
            }
            return false;
        }

        public static void Remove(f_Item item)
        {
            try
            {
                item.Stop();
                Items.Remove(item);
                Save();
            }
            catch (Exception ex)
            {
                ExceptionManager.TraceWrite(ex.Message, "保存配置");
            }
        }
    }
}
